<template>
  <div>
    <h1>注册用户</h1>
    <el-form :model="form">
      <el-form-item label="姓名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.gender" placeholder="请选择性别">
          <el-option
            v-for="item in genderOpt"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="头像">
        <!-- <el-input v-model="form.username"></el-input> -->
        <el-upload
          class="avatar-uploader"
          :headers="headers"
          action="http://localhost:7001/api/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getRegister } from '@/api'
export default {
  data() {
    return {
      genderOpt: [
        {
          label: '女',
          value: 0,
        },
        {
          label: '男',
          value: 1,
        },
      ],
      form: {
        username: '',
        password: '',
        nickname: '',
        gender: '',
        avatar: '',
      },
      imageUrl: '',
      headers: {
        token: localStorage.getItem('token'),
      },
    }
  },
  methods: {
    async onSubmit() {
      const registerData = await getRegister({
        ...this.form,
        avatar: this.imageUrl,
      })
      console.log('registerData:', registerData)
      console.log(this.form, 'form')
    },
    handleAvatarSuccess(res, file) {
      console.log(res, 'res-success')
      // this.imageUrl = URL.createObjectURL(file.raw)
      this.imageUrl = res.url
      console.log(this.imageUrl, 'imgurl')
    },
    beforeAvatarUpload(file) {
      console.log('file:', file)
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      // }
      // if (!isLt2M) {
      //   this.$message.error('上传头像图片大小不能超过 2MB!')
      // }
      return isJPG && isLt2M
    },
  },
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
